<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<title>Source code</title>
<link rel="stylesheet" type="text/css" href="../../../../../stylesheet.css" title="Style">
</head>
<body>
<div class="sourceContainer">
<pre><span class="sourceLineNo">001</span><a name="line.1"></a>
<span class="sourceLineNo">002</span>/*<a name="line.2"></a>
<span class="sourceLineNo">003</span> * Copyright (C) 2011 Archie L. Cobbs. All rights reserved.<a name="line.3"></a>
<span class="sourceLineNo">004</span> *<a name="line.4"></a>
<span class="sourceLineNo">005</span> * $Id: StringEncoder.html 852 2014-03-19 16:40:39Z archie.cobbs $<a name="line.5"></a>
<span class="sourceLineNo">006</span> */<a name="line.6"></a>
<span class="sourceLineNo">007</span><a name="line.7"></a>
<span class="sourceLineNo">008</span>package org.dellroad.stuff.string;<a name="line.8"></a>
<span class="sourceLineNo">009</span><a name="line.9"></a>
<span class="sourceLineNo">010</span>import java.util.regex.Matcher;<a name="line.10"></a>
<span class="sourceLineNo">011</span>import java.util.regex.Pattern;<a name="line.11"></a>
<span class="sourceLineNo">012</span><a name="line.12"></a>
<span class="sourceLineNo">013</span>/**<a name="line.13"></a>
<span class="sourceLineNo">014</span> * Encodes/decodes Java strings, escaping control and XML-invalid characters.<a name="line.14"></a>
<span class="sourceLineNo">015</span> */<a name="line.15"></a>
<span class="sourceLineNo">016</span>public final class StringEncoder {<a name="line.16"></a>
<span class="sourceLineNo">017</span><a name="line.17"></a>
<span class="sourceLineNo">018</span>    /**<a name="line.18"></a>
<span class="sourceLineNo">019</span>     * A regular expression that matches exactly the set of valid {@link #encode encode()}'d strings.<a name="line.19"></a>
<span class="sourceLineNo">020</span>     */<a name="line.20"></a>
<span class="sourceLineNo">021</span>    public static final Pattern ENCODE_PATTERN<a name="line.21"></a>
<span class="sourceLineNo">022</span>      = Pattern.compile("([\\t\\r\\n\\x20-\\x5b\\x5d-\\ud7ff]|[\\ue000-\\ufffd]"<a name="line.22"></a>
<span class="sourceLineNo">023</span>        + "|\\\\([\\\\bftrn]|u[\\p{XDigit}]{4}))*");<a name="line.23"></a>
<span class="sourceLineNo">024</span><a name="line.24"></a>
<span class="sourceLineNo">025</span>    /**<a name="line.25"></a>
<span class="sourceLineNo">026</span>     * A regular expression that matches exactly the set of valid {@link #enquote enquote()}'d strings.<a name="line.26"></a>
<span class="sourceLineNo">027</span>     */<a name="line.27"></a>
<span class="sourceLineNo">028</span>    public static final Pattern ENQUOTE_PATTERN<a name="line.28"></a>
<span class="sourceLineNo">029</span>      = Pattern.compile("\"([\\t\\r\\n\\x20\\x21\\x23-\\x5b\\x5d-\\ud7ff]|[\\ue000-\\ufffd]"<a name="line.29"></a>
<span class="sourceLineNo">030</span>        + "|\\\\([\\\\bftrn\"]|u[\\p{XDigit}]{4}))*\"");<a name="line.30"></a>
<span class="sourceLineNo">031</span><a name="line.31"></a>
<span class="sourceLineNo">032</span>    private static final char[] HEXDIGITS = "0123456789abcdef".toCharArray();<a name="line.32"></a>
<span class="sourceLineNo">033</span><a name="line.33"></a>
<span class="sourceLineNo">034</span>    private StringEncoder() {<a name="line.34"></a>
<span class="sourceLineNo">035</span>    }<a name="line.35"></a>
<span class="sourceLineNo">036</span><a name="line.36"></a>
<span class="sourceLineNo">037</span>    /**<a name="line.37"></a>
<span class="sourceLineNo">038</span>     * Encode a string, escaping control and XML-invalid characters.<a name="line.38"></a>
<span class="sourceLineNo">039</span>     * Whether tab, newline, and carriage return are escaped is optional;<a name="line.39"></a>
<span class="sourceLineNo">040</span>     * these are the three control characters that are valid inside XML documents.<a name="line.40"></a>
<span class="sourceLineNo">041</span>     * &lt;p/&gt;<a name="line.41"></a>
<span class="sourceLineNo">042</span>     * &lt;p/&gt;<a name="line.42"></a>
<span class="sourceLineNo">043</span>     * Characters are escaped using &lt;code&gt;&amp;#92;uNNNN&lt;/code&gt; notation like Java unicode characters,<a name="line.43"></a>
<span class="sourceLineNo">044</span>     * e.g., &lt;code&gt;0x001f&lt;/code&gt; would appear in the encoded string as &lt;code&gt;&amp;#92;u001f&lt;/code&gt;.<a name="line.44"></a>
<span class="sourceLineNo">045</span>     * Normal Java backslash escapes are used for tab, newline, carriage return, backspace, and formfeed.<a name="line.45"></a>
<span class="sourceLineNo">046</span>     * Backslash characters are themselves encoded with a double backslash.<a name="line.46"></a>
<span class="sourceLineNo">047</span>     *<a name="line.47"></a>
<span class="sourceLineNo">048</span>     * @param value         string to encode (possibly null)<a name="line.48"></a>
<span class="sourceLineNo">049</span>     * @param escapeTABNLCR escape tab, newline, and carriage return characters as well<a name="line.49"></a>
<span class="sourceLineNo">050</span>     * @return the encoded version of {@code value}, or {@code null} if {@code value} was {@code null}<a name="line.50"></a>
<span class="sourceLineNo">051</span>     * @see #decode<a name="line.51"></a>
<span class="sourceLineNo">052</span>     * @see #isValidXMLChar<a name="line.52"></a>
<span class="sourceLineNo">053</span>     */<a name="line.53"></a>
<span class="sourceLineNo">054</span>    public static String encode(String value, boolean escapeTABNLCR) {<a name="line.54"></a>
<span class="sourceLineNo">055</span>        if (value == null)<a name="line.55"></a>
<span class="sourceLineNo">056</span>            return value;<a name="line.56"></a>
<span class="sourceLineNo">057</span>        StringBuilder buf = new StringBuilder(value.length() + 4);<a name="line.57"></a>
<span class="sourceLineNo">058</span>        final int limit = value.length();<a name="line.58"></a>
<span class="sourceLineNo">059</span>        for (int i = 0; i &lt; limit; i++) {<a name="line.59"></a>
<span class="sourceLineNo">060</span>            final char ch = value.charAt(i);<a name="line.60"></a>
<span class="sourceLineNo">061</span><a name="line.61"></a>
<span class="sourceLineNo">062</span>            // Handle special escapes<a name="line.62"></a>
<span class="sourceLineNo">063</span>            switch (ch) {<a name="line.63"></a>
<span class="sourceLineNo">064</span>            case '\\':<a name="line.64"></a>
<span class="sourceLineNo">065</span>                buf.append('\\').append('\\');<a name="line.65"></a>
<span class="sourceLineNo">066</span>                continue;<a name="line.66"></a>
<span class="sourceLineNo">067</span>            case '\b':<a name="line.67"></a>
<span class="sourceLineNo">068</span>                buf.append('\\').append('b');<a name="line.68"></a>
<span class="sourceLineNo">069</span>                continue;<a name="line.69"></a>
<span class="sourceLineNo">070</span>            case '\f':<a name="line.70"></a>
<span class="sourceLineNo">071</span>                buf.append('\\').append('f');<a name="line.71"></a>
<span class="sourceLineNo">072</span>                continue;<a name="line.72"></a>
<span class="sourceLineNo">073</span>            case '\t':<a name="line.73"></a>
<span class="sourceLineNo">074</span>                if (escapeTABNLCR) {<a name="line.74"></a>
<span class="sourceLineNo">075</span>                    buf.append('\\').append('t');<a name="line.75"></a>
<span class="sourceLineNo">076</span>                    continue;<a name="line.76"></a>
<span class="sourceLineNo">077</span>                }<a name="line.77"></a>
<span class="sourceLineNo">078</span>                break;<a name="line.78"></a>
<span class="sourceLineNo">079</span>            case '\n':<a name="line.79"></a>
<span class="sourceLineNo">080</span>                if (escapeTABNLCR) {<a name="line.80"></a>
<span class="sourceLineNo">081</span>                    buf.append('\\').append('n');<a name="line.81"></a>
<span class="sourceLineNo">082</span>                    continue;<a name="line.82"></a>
<span class="sourceLineNo">083</span>                }<a name="line.83"></a>
<span class="sourceLineNo">084</span>                break;<a name="line.84"></a>
<span class="sourceLineNo">085</span>            case '\r':<a name="line.85"></a>
<span class="sourceLineNo">086</span>                if (escapeTABNLCR) {<a name="line.86"></a>
<span class="sourceLineNo">087</span>                    buf.append('\\').append('r');<a name="line.87"></a>
<span class="sourceLineNo">088</span>                    continue;<a name="line.88"></a>
<span class="sourceLineNo">089</span>                }<a name="line.89"></a>
<span class="sourceLineNo">090</span>                break;<a name="line.90"></a>
<span class="sourceLineNo">091</span>            default:<a name="line.91"></a>
<span class="sourceLineNo">092</span>                break;<a name="line.92"></a>
<span class="sourceLineNo">093</span>            }<a name="line.93"></a>
<span class="sourceLineNo">094</span><a name="line.94"></a>
<span class="sourceLineNo">095</span>            // If character is an otherwise valid XML character, pass it through unchanged<a name="line.95"></a>
<span class="sourceLineNo">096</span>            if (isValidXMLChar(ch)) {<a name="line.96"></a>
<span class="sourceLineNo">097</span>                buf.append(ch);<a name="line.97"></a>
<span class="sourceLineNo">098</span>                continue;<a name="line.98"></a>
<span class="sourceLineNo">099</span>            }<a name="line.99"></a>
<span class="sourceLineNo">100</span><a name="line.100"></a>
<span class="sourceLineNo">101</span>            // Escape it using 4 digit hex<a name="line.101"></a>
<span class="sourceLineNo">102</span>            buf.append('\\');<a name="line.102"></a>
<span class="sourceLineNo">103</span>            buf.append('u');<a name="line.103"></a>
<span class="sourceLineNo">104</span>            for (int shift = 12; shift &gt;= 0; shift -= 4)<a name="line.104"></a>
<span class="sourceLineNo">105</span>                buf.append(HEXDIGITS[(ch &gt;&gt; shift) &amp; 0x0f]);<a name="line.105"></a>
<span class="sourceLineNo">106</span>        }<a name="line.106"></a>
<span class="sourceLineNo">107</span>        return buf.toString();<a name="line.107"></a>
<span class="sourceLineNo">108</span>    }<a name="line.108"></a>
<span class="sourceLineNo">109</span><a name="line.109"></a>
<span class="sourceLineNo">110</span>    /**<a name="line.110"></a>
<span class="sourceLineNo">111</span>     * Decode a string encoded by {@link #encode}.<a name="line.111"></a>
<span class="sourceLineNo">112</span>     * &lt;p/&gt;<a name="line.112"></a>
<span class="sourceLineNo">113</span>     * &lt;p/&gt;<a name="line.113"></a>
<span class="sourceLineNo">114</span>     * The parsing is strict; any ill-formed backslash escape sequence (i.e., not of the form<a name="line.114"></a>
<span class="sourceLineNo">115</span>     * &lt;code&gt;&amp;#92;uNNNN&lt;/code&gt;, &lt;code&gt;\b&lt;/code&gt;, &lt;code&gt;\t&lt;/code&gt;, &lt;code&gt;\n&lt;/code&gt;, &lt;code&gt;\f&lt;/code&gt;, &lt;code&gt;\r&lt;/code&gt;<a name="line.115"></a>
<span class="sourceLineNo">116</span>     * or &lt;code&gt;\\&lt;/code&gt;) will cause an exception to be thrown.<a name="line.116"></a>
<span class="sourceLineNo">117</span>     *<a name="line.117"></a>
<span class="sourceLineNo">118</span>     * @param text string to decode (possibly null)<a name="line.118"></a>
<span class="sourceLineNo">119</span>     * @return the decoded version of {@code text}, or {@code null} if {@code text} was {@code null}<a name="line.119"></a>
<span class="sourceLineNo">120</span>     * @throws IllegalArgumentException if {@code text} contains an invalid escape sequence<a name="line.120"></a>
<span class="sourceLineNo">121</span>     * @see #encode<a name="line.121"></a>
<span class="sourceLineNo">122</span>     */<a name="line.122"></a>
<span class="sourceLineNo">123</span>    public static String decode(String text) {<a name="line.123"></a>
<span class="sourceLineNo">124</span>        if (text == null)<a name="line.124"></a>
<span class="sourceLineNo">125</span>            return null;<a name="line.125"></a>
<span class="sourceLineNo">126</span>        StringBuilder buf = new StringBuilder(text.length());<a name="line.126"></a>
<span class="sourceLineNo">127</span>        final int limit = text.length();<a name="line.127"></a>
<span class="sourceLineNo">128</span>        for (int i = 0; i &lt; limit; i++) {<a name="line.128"></a>
<span class="sourceLineNo">129</span>            char ch = text.charAt(i);<a name="line.129"></a>
<span class="sourceLineNo">130</span><a name="line.130"></a>
<span class="sourceLineNo">131</span>            // Handle unescaped characters<a name="line.131"></a>
<span class="sourceLineNo">132</span>            if (ch != '\\') {<a name="line.132"></a>
<span class="sourceLineNo">133</span>                if (!isValidXMLChar(ch))<a name="line.133"></a>
<span class="sourceLineNo">134</span>                    throw new IllegalArgumentException(String.format("illegal character 0x%04x in encoded string", ch));<a name="line.134"></a>
<span class="sourceLineNo">135</span>                buf.append(ch);<a name="line.135"></a>
<span class="sourceLineNo">136</span>                continue;<a name="line.136"></a>
<span class="sourceLineNo">137</span>            }<a name="line.137"></a>
<span class="sourceLineNo">138</span><a name="line.138"></a>
<span class="sourceLineNo">139</span>            // Get next char<a name="line.139"></a>
<span class="sourceLineNo">140</span>            if (++i &gt;= limit)<a name="line.140"></a>
<span class="sourceLineNo">141</span>                throw new IllegalArgumentException("illegal trailing '\\' in encoded string");<a name="line.141"></a>
<span class="sourceLineNo">142</span>            ch = text.charAt(i);<a name="line.142"></a>
<span class="sourceLineNo">143</span><a name="line.143"></a>
<span class="sourceLineNo">144</span>            // Check for special escapes<a name="line.144"></a>
<span class="sourceLineNo">145</span>            switch (ch) {<a name="line.145"></a>
<span class="sourceLineNo">146</span>            case '\\':<a name="line.146"></a>
<span class="sourceLineNo">147</span>                buf.append('\\');<a name="line.147"></a>
<span class="sourceLineNo">148</span>                continue;<a name="line.148"></a>
<span class="sourceLineNo">149</span>            case 'b':<a name="line.149"></a>
<span class="sourceLineNo">150</span>                buf.append('\b');<a name="line.150"></a>
<span class="sourceLineNo">151</span>                continue;<a name="line.151"></a>
<span class="sourceLineNo">152</span>            case 't':<a name="line.152"></a>
<span class="sourceLineNo">153</span>                buf.append('\t');<a name="line.153"></a>
<span class="sourceLineNo">154</span>                continue;<a name="line.154"></a>
<span class="sourceLineNo">155</span>            case 'n':<a name="line.155"></a>
<span class="sourceLineNo">156</span>                buf.append('\n');<a name="line.156"></a>
<span class="sourceLineNo">157</span>                continue;<a name="line.157"></a>
<span class="sourceLineNo">158</span>            case 'f':<a name="line.158"></a>
<span class="sourceLineNo">159</span>                buf.append('\f');<a name="line.159"></a>
<span class="sourceLineNo">160</span>                continue;<a name="line.160"></a>
<span class="sourceLineNo">161</span>            case 'r':<a name="line.161"></a>
<span class="sourceLineNo">162</span>                buf.append('\r');<a name="line.162"></a>
<span class="sourceLineNo">163</span>                continue;<a name="line.163"></a>
<span class="sourceLineNo">164</span>            default:<a name="line.164"></a>
<span class="sourceLineNo">165</span>                break;<a name="line.165"></a>
<span class="sourceLineNo">166</span>            }<a name="line.166"></a>
<span class="sourceLineNo">167</span><a name="line.167"></a>
<span class="sourceLineNo">168</span>            // Must be unicode escape<a name="line.168"></a>
<span class="sourceLineNo">169</span>            if (ch != 'u')<a name="line.169"></a>
<span class="sourceLineNo">170</span>                throw new IllegalArgumentException("illegal escape sequence '\\" + ch + "' in encoded string");<a name="line.170"></a>
<span class="sourceLineNo">171</span><a name="line.171"></a>
<span class="sourceLineNo">172</span>            // Decode hex value<a name="line.172"></a>
<span class="sourceLineNo">173</span>            if (i + 4 &gt;= limit)<a name="line.173"></a>
<span class="sourceLineNo">174</span>                throw new IllegalArgumentException("illegal truncated '\\u' escape sequence in encoded string");<a name="line.174"></a>
<span class="sourceLineNo">175</span>            int value = 0;<a name="line.175"></a>
<span class="sourceLineNo">176</span>            for (int j = 0; j &lt; 4; j++) {<a name="line.176"></a>
<span class="sourceLineNo">177</span>                int nibble = Character.digit(text.charAt(++i), 16);<a name="line.177"></a>
<span class="sourceLineNo">178</span>                if (nibble == -1) {<a name="line.178"></a>
<span class="sourceLineNo">179</span>                    throw new IllegalArgumentException(<a name="line.179"></a>
<span class="sourceLineNo">180</span>                      "illegal escape sequence '" + text.substring(i - j - 2, i - j + 4) + "' in encoded string");<a name="line.180"></a>
<span class="sourceLineNo">181</span>                }<a name="line.181"></a>
<span class="sourceLineNo">182</span>                assert nibble &gt;= 0 &amp;&amp; nibble &lt;= 0xf;<a name="line.182"></a>
<span class="sourceLineNo">183</span>                value = (value &lt;&lt; 4) | nibble;<a name="line.183"></a>
<span class="sourceLineNo">184</span>            }<a name="line.184"></a>
<span class="sourceLineNo">185</span><a name="line.185"></a>
<span class="sourceLineNo">186</span>            // Append decodec character<a name="line.186"></a>
<span class="sourceLineNo">187</span>            buf.append((char)value);<a name="line.187"></a>
<span class="sourceLineNo">188</span>        }<a name="line.188"></a>
<span class="sourceLineNo">189</span>        return buf.toString();<a name="line.189"></a>
<span class="sourceLineNo">190</span>    }<a name="line.190"></a>
<span class="sourceLineNo">191</span><a name="line.191"></a>
<span class="sourceLineNo">192</span>    /**<a name="line.192"></a>
<span class="sourceLineNo">193</span>     * Enquote a string. Functions like {@link #encode encode(string, true)} but in addition the resulting<a name="line.193"></a>
<span class="sourceLineNo">194</span>     * string is surrounded by double quotes and double quotes in the string are backslash-escaped.<a name="line.194"></a>
<span class="sourceLineNo">195</span>     * &lt;p/&gt;<a name="line.195"></a>
<span class="sourceLineNo">196</span>     * &lt;p&gt;<a name="line.196"></a>
<span class="sourceLineNo">197</span>     * Note: the strings returned by this method are not suitable for decoding by {@link #decode}.<a name="line.197"></a>
<span class="sourceLineNo">198</span>     * Use {@link #dequote} instead.<a name="line.198"></a>
<span class="sourceLineNo">199</span>     * &lt;/p&gt;<a name="line.199"></a>
<span class="sourceLineNo">200</span>     */<a name="line.200"></a>
<span class="sourceLineNo">201</span>    public static String enquote(String string) {<a name="line.201"></a>
<span class="sourceLineNo">202</span>        return '"' + encode(string, true).replaceAll(Pattern.quote("\""), Matcher.quoteReplacement("\\\"")) + '"';<a name="line.202"></a>
<span class="sourceLineNo">203</span>    }<a name="line.203"></a>
<span class="sourceLineNo">204</span><a name="line.204"></a>
<span class="sourceLineNo">205</span>    /**<a name="line.205"></a>
<span class="sourceLineNo">206</span>     * Enquote bytes, treating them as an ASCII string.<a name="line.206"></a>
<span class="sourceLineNo">207</span>     *<a name="line.207"></a>
<span class="sourceLineNo">208</span>     * @see #enquote(String)<a name="line.208"></a>
<span class="sourceLineNo">209</span>     */<a name="line.209"></a>
<span class="sourceLineNo">210</span>    public static String enquote(byte[] data, int off, int len) {<a name="line.210"></a>
<span class="sourceLineNo">211</span>        char[] chars = new char[len];<a name="line.211"></a>
<span class="sourceLineNo">212</span>        for (int i = 0; i &lt; len; i++)<a name="line.212"></a>
<span class="sourceLineNo">213</span>            chars[i] = (char)(data[off + i] &amp; 0xff);<a name="line.213"></a>
<span class="sourceLineNo">214</span>        return enquote(new String(chars));<a name="line.214"></a>
<span class="sourceLineNo">215</span>    }<a name="line.215"></a>
<span class="sourceLineNo">216</span><a name="line.216"></a>
<span class="sourceLineNo">217</span>    /**<a name="line.217"></a>
<span class="sourceLineNo">218</span>     * Dequote a string previously enquoted by {@link #enquote}.<a name="line.218"></a>
<span class="sourceLineNo">219</span>     *<a name="line.219"></a>
<span class="sourceLineNo">220</span>     * @param quotedString a string returned by {@link #enquote}<a name="line.220"></a>
<span class="sourceLineNo">221</span>     * @throws IllegalArgumentException if {@code quotedString} has an invalid format (i.e., it could not have<a name="line.221"></a>
<span class="sourceLineNo">222</span>     *  ever been returned by {@link #enquote})<a name="line.222"></a>
<span class="sourceLineNo">223</span>     */<a name="line.223"></a>
<span class="sourceLineNo">224</span>    public static String dequote(String quotedString) {<a name="line.224"></a>
<span class="sourceLineNo">225</span>        int len = quotedString.length();<a name="line.225"></a>
<span class="sourceLineNo">226</span>        if (len &lt; 2 || quotedString.charAt(0) != '"' || quotedString.charAt(len - 1) != '"')<a name="line.226"></a>
<span class="sourceLineNo">227</span>            throw new IllegalArgumentException("invalid quoted string: not surrounded by quote characters");<a name="line.227"></a>
<span class="sourceLineNo">228</span>        quotedString = quotedString.substring(1, len - 1);<a name="line.228"></a>
<span class="sourceLineNo">229</span>        if (quotedString.matches("(?s)^(\"|.*[^\\\\]\").*$"))<a name="line.229"></a>
<span class="sourceLineNo">230</span>            throw new IllegalArgumentException("invalid quoted string: unescaped nested quote character");<a name="line.230"></a>
<span class="sourceLineNo">231</span>        quotedString = quotedString.replaceAll(Pattern.quote("\\\""), Matcher.quoteReplacement("\""));<a name="line.231"></a>
<span class="sourceLineNo">232</span>        return decode(quotedString);<a name="line.232"></a>
<span class="sourceLineNo">233</span>    }<a name="line.233"></a>
<span class="sourceLineNo">234</span><a name="line.234"></a>
<span class="sourceLineNo">235</span>    /**<a name="line.235"></a>
<span class="sourceLineNo">236</span>     * Determine the length of a string previously enquoted by {@link #enquote} when it appears<a name="line.236"></a>
<span class="sourceLineNo">237</span>     * as the prefix of a longer string. This method assumes that the prefix is a valid quoted<a name="line.237"></a>
<span class="sourceLineNo">238</span>     * string; use {@link #dequote} to verify.<a name="line.238"></a>
<span class="sourceLineNo">239</span>     *<a name="line.239"></a>
<span class="sourceLineNo">240</span>     * @param string a string containing a prefix returned by {@link #enquote}<a name="line.240"></a>
<span class="sourceLineNo">241</span>     * @throws IllegalArgumentException if a starting or terminating quote character is not found<a name="line.241"></a>
<span class="sourceLineNo">242</span>     */<a name="line.242"></a>
<span class="sourceLineNo">243</span>    public static int enquotedLength(String string) {<a name="line.243"></a>
<span class="sourceLineNo">244</span>        int len = string.length();<a name="line.244"></a>
<span class="sourceLineNo">245</span>        if (len == 0 || string.charAt(0) != '"')<a name="line.245"></a>
<span class="sourceLineNo">246</span>            throw new IllegalArgumentException("invalid quoted string prefix: string does not begin with a quote character");<a name="line.246"></a>
<span class="sourceLineNo">247</span>        for (int i = 1; i &lt; len; i++) {<a name="line.247"></a>
<span class="sourceLineNo">248</span>            if (string.charAt(i) == '"' &amp;&amp; string.charAt(i - 1) != '\\')<a name="line.248"></a>
<span class="sourceLineNo">249</span>                return i + 1;<a name="line.249"></a>
<span class="sourceLineNo">250</span>        }<a name="line.250"></a>
<span class="sourceLineNo">251</span>        throw new IllegalArgumentException("invalid quoted string prefix: no terminating quote character found");<a name="line.251"></a>
<span class="sourceLineNo">252</span>    }<a name="line.252"></a>
<span class="sourceLineNo">253</span><a name="line.253"></a>
<span class="sourceLineNo">254</span>    /**<a name="line.254"></a>
<span class="sourceLineNo">255</span>     * Determine if the given character is a valid XML character according to the XML 1.0 specification.<a name="line.255"></a>
<span class="sourceLineNo">256</span>     * &lt;p/&gt;<a name="line.256"></a>
<span class="sourceLineNo">257</span>     * &lt;p&gt;<a name="line.257"></a>
<span class="sourceLineNo">258</span>     * Valid characters are tab, newline, carriage return, and characters in the ranges<a name="line.258"></a>
<span class="sourceLineNo">259</span>     * &lt;code&gt;&amp;#92;u0020 - &amp;#92;ud7ff&lt;/code&gt; and &lt;code&gt;&amp;#92;ue000 - &amp;#92;fffdf&lt;/code&gt; (inclusive).<a name="line.259"></a>
<span class="sourceLineNo">260</span>     * &lt;/p&gt;<a name="line.260"></a>
<span class="sourceLineNo">261</span>     *<a name="line.261"></a>
<span class="sourceLineNo">262</span>     * @see &lt;a href="http://www.w3.org/TR/REC-xml/#charsets"&gt;The XML 1.0 Specification&lt;/a&gt;<a name="line.262"></a>
<span class="sourceLineNo">263</span>     */<a name="line.263"></a>
<span class="sourceLineNo">264</span>    public static boolean isValidXMLChar(char ch) {<a name="line.264"></a>
<span class="sourceLineNo">265</span>        return (ch &gt;= '\u0020' &amp;&amp; ch &lt;= '\ud7ff') || ch == '\n' || ch == '\r' || ch == '\t' || (ch &gt;= '\ue000' &amp;&amp; ch &lt;= '\ufffd');<a name="line.265"></a>
<span class="sourceLineNo">266</span>    }<a name="line.266"></a>
<span class="sourceLineNo">267</span>}<a name="line.267"></a>
<span class="sourceLineNo">268</span><a name="line.268"></a>




























































</pre>
</div>
</body>
</html>
